<?php
class PostsViewModel extends Model
{
	protected $viewModel = true;
	
	var $attachList;
	var $commentList;
	
	// 视图模型中的字段定义 
	protected $viewFields = array( 
		"Posts"=>array("ID","post_author","post_date","post_content","post_title","top","post_name","post_type","comment_count","comment_status","ping_status"),
		"Category"=>array("cid","name","slug"),
		"Usermeta"=>array("display_name"),
	); 

	// 视图关联的where条件 
	protected $viewCondition = array(
		"Posts.post_category" => array("eqf","Category.cid"),
		"Posts.post_author" => array("eqf","Usermeta.user_id")
	);
	
	// 与附件表相关联,每篇日志有多个附件
	var $_link = array(
		array('mapping_type'=>HAS_MANY,
			'class_name'=>'Attach',
			'foreign_key'=>'`postId`',
			'condition'=>"`inserted`='0'",
			'mapping_name'=>'attachList'),
		array('mapping_type'=>HAS_MANY,
			'class_name'=>'Comments',
			'foreign_key'=>'comment_post_ID',
			'mapping_name'=>'commentList')
	);
	
	// 视图模型的主键
	Public function getPk() { 
		return "ID"; 
	}
	
	function __construct()
	{
		parent::__construct();
		$options = S("options"); //先完成基类的构造再加载options的缓存
		$this->_link[1]['mapping_order'] = "comment_date ".$options["comments_order"];
	}
}
?>